//
//  VCOfSetUp.m
//  XYFMApp
//
//  Created by 岁变 on 16/9/18.
//  Copyright © 2016年 岁变. All rights reserved.
//

#import "VCOfSetUp.h"
#import "XYMacro.h"

@interface VCOfSetUp () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) NSArray *arr;
@property (nonatomic, strong) UIView *customPromptBox;

@end

@implementation VCOfSetUp

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configNavigationController];
    [self configViewController];
    [self createPromptBox];

}

- (void)configNavigationController {
    self.title = @"设置";
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    UIButton *goBackButton = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackButton.frame = CGRectMake(0, 0, 30, 30);
    [goBackButton setImage:[UIImage imageNamed:@"go_back"] forState:UIControlStateNormal];
    [goBackButton addTarget:self action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:goBackButton];
    self.navigationItem.leftBarButtonItem = leftItem;
}

- (void)goBack:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.tabBarController.tabBar.hidden = NO;
}


- (void)configViewController {
    
    _arr = @[@"清除缓存", @"检查更新", @"关于软件", @"意见反馈"];
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    backView.backgroundColor = BACKGROUNDCOLOR;
    [self.view addSubview:backView];
    
    UIButton *buttonExit = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonExit.backgroundColor = [UIColor redColor];
    [buttonExit setTitle:@"退出登录" forState:UIControlStateNormal];
    [buttonExit setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    buttonExit.frame = CGRectMake(0, backView.frame.size.height - 100, SCREEN_WIDTH, 50);
    [buttonExit addTarget:self action:@selector(exitAction:) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:buttonExit];
    
    _tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300) style:UITableViewStylePlain];
    _tableView.backgroundColor = BACKGROUNDCOLOR;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.scrollEnabled = NO;
    [_tableView registerClass:[VCOfRegisterTableViewCellFirst class] forCellReuseIdentifier:@"VCOfRegisterTableViewCellFirst"];
    [backView addSubview:_tableView];
    
}

- (void)exitAction:(UIButton *)button {
    _customPromptBox.hidden = NO;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    VCOfRegisterTableViewCellFirst *cell = [tableView dequeueReusableCellWithIdentifier:@"VCOfRegisterTableViewCellFirst" forIndexPath:indexPath];
    cell.label.text = [_arr objectAtIndex:indexPath.row];
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    if (indexPath.row == 0) {
        //清除缓存
        [self createAlertController];
        
    }
}


#pragma mark -----------创建自定义提示栏-----------------
- (void)createPromptBox {
    UIWindow *windows = [UIApplication sharedApplication].keyWindow;
    _customPromptBox = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 150)];
    _customPromptBox.center = windows.center;
    _customPromptBox.backgroundColor = [UIColor lightGrayColor];
    [windows addSubview:_customPromptBox];
    _customPromptBox.layer.borderWidth = 2;
    _customPromptBox.layer.borderColor = [UIColor redColor].CGColor;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 99)];
    label.backgroundColor = BACKGROUNDCOLOR;
    label.text = @"确定退出该账户";
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    [_customPromptBox addSubview:label];
    
    UIButton *buttonOne = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonOne.backgroundColor = BACKGROUNDCOLOR;
    [buttonOne setTitle:@"否" forState:UIControlStateNormal];
    [buttonOne setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    buttonOne.frame = CGRectMake(0, 100, 149, 50);
    [buttonOne addTarget:self action:@selector(clickButtonOne:) forControlEvents:UIControlEventTouchUpInside];
    [_customPromptBox addSubview:buttonOne];
    
    
    UIButton *buttonTwo = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonTwo.backgroundColor = BACKGROUNDCOLOR;
    [buttonTwo setTitle:@"是" forState:UIControlStateNormal];
    [buttonTwo setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    buttonTwo.frame = CGRectMake(150, 100, 150, 50);
    [buttonTwo addTarget:self action:@selector(clickButtonTwo:) forControlEvents:UIControlEventTouchUpInside];
    [_customPromptBox addSubview:buttonTwo];
    _customPromptBox.hidden = YES;
    
}

- (void)clickButtonOne:(UIButton *)button {
    
    _customPromptBox.hidden = YES;
    
}

- (void)clickButtonTwo:(UIButton *)button {
    
    VCOfMine *nextVC = [[VCOfMine alloc] init];
    [self.navigationController pushViewController:nextVC animated:YES];
    _customPromptBox.hidden = YES;
    self.tabBarController.tabBar.hidden = NO;
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftView.backgroundColor = [UIColor blackColor];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftView];
    nextVC.navigationItem.leftBarButtonItem = leftItem;

    self.navigationController.viewControllers = @[nextVC];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user removeObjectForKey:@"password"];
    [user removeObjectForKey:@"phoneNumber"];
    
}





#pragma mark -  清除缓存功能实现
// 创建UIAlertController
- (void)createAlertController
{
    
    float cacheSize = [self currentCacheSize];
    
    //显示提示框.
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"缓存大小为%.2fM,确定要清理?", cacheSize] preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self ClearCache];
    }];
    [alertController addAction:actionCancel];
    [alertController addAction:actionOk];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

// 计算清除的缓存
- (float)currentCacheSize {
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    return [self folderSizeAtPath:cachesPath];
}


// 遍历文件夹获得文件夹大小, 返回多少m
- (float) folderSizeAtPath:(NSString *)folderPath
{
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath])
    {
        return 0;
    }
    else
    {
        NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
        NSString *fileName;
        long long folderSize = 0;
        while ((fileName = [childFilesEnumerator nextObject]) != nil)
        {
            NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
            folderSize += [self fileSizeAtPath:fileAbsolutePath];
        }
        
        return folderSize / (1024.0 * 1024.0);
    }
}

// 计算单个文件的大小
- (long long)fileSizeAtPath:(NSString *)filePath
{
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]) {
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }else{
        return 0;
    }
}
// 清除缓存
- (void)ClearCache
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *path = [paths lastObject];
    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:path];
    for (NSString *p in files)
    {
        NSError *error;
        NSString *Path = [path stringByAppendingPathComponent:p];
        if ([[NSFileManager defaultManager] fileExistsAtPath:Path])
        {
            [[NSFileManager defaultManager] removeItemAtPath:Path error:&error];
        }
    }
}




- (void)viewDidAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
